package com.example.fragment;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onclick();
    }
public void intview(){
  //获取FragmentManager
    FragmentManager fm=getSupportFragmentManager();
    //获取 FragmentTransaction
    FragmentTransaction ft=fm.beginTransaction();
    //添加Fragment
    ft.add(R.id.frane_df,new DeartFragment());
    //移除
   // ft.remove(new DeartFragment());
    //替换
    //ft.replace(R.id.frane_df,new DeartFragment());
    //显示
    //ft.show(new DeartFragment());
    //隐藏
    //ft.hide(new DeartFragment())
            //提交
    //同一个 FragmentTransaction只可以提交一次，不可重复操作
    ft.commit();
    //为了避免 FragmentTransaction重复使用
    getSupportFragmentManager().beginTransaction().add(R.id.frane_df,new DeartFragment());

}
public void onclick(){
    ViewPager viewPager=findViewById(R.id.view_pager);
    List<Fragment> fragments=new ArrayList<>();
    fragments.add(new BlankFragment());
    fragments.add(new DeartFragment());
    fragments.add(new SeartFragment());

    viewAdaper adaper=new viewAdaper(getSupportFragmentManager(),FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    adaper.setList(fragments);
    viewPager.setAdapter(adaper);

    }
    class viewAdaper extends FragmentPagerAdapter{
        private List<Fragment> list;

        public void setList(List<Fragment> list) {
            this.list = list;
        }

        public viewAdaper(@NonNull  FragmentManager fm, int behavior) {
            super(fm, behavior);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }


        @Override
        public int getCount() {
            return null!=list?list.size():0;
        }

}
}